<?php

namespace app\api\controller;
use app\common\model\User;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use app\common\library\Ems as Emslib;
/**
 * 會員接口
 */
class Auth extends Api
{
    protected $noNeedLogin = ['login','vaildcode'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }
    /**
     * 用戶登錄
     *
     * @ApiMethod (POST)
     * @ApiParams (name="account", type="string", required=true, description="賬號")
     * @ApiParams (name="password", type="string", required=true, description="密碼")
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        file_put_contents("aaa.log",$account.$password);
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }
    

    /**
     * 註冊會員
     *
     * @ApiMethod (POST)
     * @ApiParams (name="username", type="string", required=true, description="用戶名")
     * @ApiParams (name="password", type="string", required=true, description="密碼")
     * @ApiParams (name="email", type="string", required=true, description="郵箱")
     * @ApiParams (name="mobile", type="string", required=true, description="手機號")
     * @ApiParams (name="code", type="string", required=true, description="驗證碼")
     */
    public function register()
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        $email = $this->request->post('email');
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');
        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }
        if ($email && !Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $ret = Sms::check($mobile, $code, 'register');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $ret = $this->auth->register($username, $password, $email, $mobile, []);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Sign up successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }
    /**
     * 退出登錄
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }
    // 驗證
    public function vaildcode(){
        $event = $this->request->post('event');
        $email = $this->request->post('email');
        $code = $this->request->post('code');
        if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $this->error(__('郵箱格式不正確'));
        }
        if (empty($code)) {
            $this->error(__('請輸入驗證碼'));
        }
        $userinfo = User::getByEmail($email);
        if(empty($userinfo)){
            $this->error(__('該郵箱未註冊'));
        }
         try{
            $ret = Emslib::check($email, $code, $event);
        }catch(\Exception $e){
            $this->error($e->getMessage());
        }
        if ($ret) {
            $this->auth->direct($userinfo['id']);
            $user = $this->auth->getUserinfo();
            $this->success(__('ok'),$user);
        } else {
            $this->error(__('驗證失敗'));
        }
    }
    // 修改密碼
    public function resetpwd(){
        $password = $this->request->post('password');
        $repassword = $this->request->post('repassword');
        if(empty($password) || empty($repassword)){
            $this->error(__('請輸入密碼'));
        }   
        if($password != $repassword){
            $this->error(__('兩次輸入密碼不一致'));
        }
        $ret = $this->auth->changepwd($password, '', true);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }
}